using System;
using Renci.SshNet;
using dotnet;
using wspace;

namespace ssh
{
    public class ScpConnect
    {
        public ScpClient scp {get;}

        public ScpConnect (string host, string user, string pwd, int port = 22)
        {
            try
            {
                ConnectionInfo cinfo = new ConnectionInfo(
                    host, port, user,
                    new PasswordAuthenticationMethod(user, pwd)
                );
                scp = new ScpClient(cinfo);
            }
            catch (Exception e)
            {
                Utils.ErrorLog(e.Message);
                System.Environment.Exit(-1);
            }
        }

        public ScpConnect (ServerInfo sinfo)
        {
            try
            {
                ConnectionInfo cinfo = new ConnectionInfo(
                    sinfo.host, sinfo.port, sinfo.user,
                    new PasswordAuthenticationMethod(sinfo.user, sinfo.pwd)
                );
                scp = new ScpClient(cinfo);
            }
            catch (Exception e)
            {
                Utils.ErrorLog(e.Message);
                System.Environment.Exit(-1);
            }
        }

        public bool Connect ()
        {
            if (!this.scp.IsConnected)
            {
                try
                {
                    this.scp.Connect();
                }
                catch (Exception e)
                {
                    Utils.ErrorLog(e.Message);
                    return false;
                }
            }
            return true;
        }

        public void DisConnect ()
        {
            if (this.scp.IsConnected)
            {
                this.scp.Disconnect();
            }
        }
    }
}